package com.fuqi.android.zhongcai.base.view.fragment;

import android.util.Log;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.application.UniApplication;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.ApiUrl;
import com.fuqi.android.zhongcai.base.request.api.BannerListApi;
import com.fuqi.android.zhongcai.base.request.result.bean.BannerBean;
import com.fuqi.android.zhongcai.business.shuig.OrderDict;
import com.fuqi.android.zhongcai.business.shuig.view.activity.ShuiGMakeOrderActivity;
import com.fuqi.android.zhongcai.business.dinuan.view.activity.DiNuanMarkOrderActivity;
import com.fuqi.android.zhongcai.base.view.activity.GongYiActivity;
import com.fuqi.android.zhongcai.base.view.activity.SanBaoActivity;
import com.fuqi.android.zhongcai.base.view.widget.AutoBanner;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Lenovo on 2016/8/29.
 */
public class HomeFragment extends BaseFragment {

    @Bind(R.id.auto_banner)
    AutoBanner autoBanner;
    private static HomeFragment fragment;

    public static HomeFragment getInstance(){
        if(fragment == null) fragment = new HomeFragment();
        return fragment;
    }

    @OnClick({R.id.lyt_san_bao,R.id.lyt_gong_yi,R.id.lyt_shui_guan,R.id.lyt_di_nuan})
    public void onClick(View view){

        String role = UniApplication.getInstance().getCurrentRole();
        String type = UniApplication.getInstance().getUserInfo().getType();
        switch (view.getId()){

            case R.id.lyt_san_bao:
                ActivityUtils.startActivity(getActivity(), SanBaoActivity.class);
                break;
            case R.id.lyt_gong_yi:
                ActivityUtils.startActivity(getActivity(), GongYiActivity.class);
                break;
            case R.id.lyt_shui_guan:
                if((OrderDict.RoleType.TYPE_NQ.equals(role) && Constants.TYPE_SHUI_GUAN.equals(type)) || OrderDict.RoleType.TYPE_GRKH.equals(role)) {
                    ActivityUtils.startActivity(getActivity(), ShuiGMakeOrderActivity.class);
                }else
                    ActivityUtils.toast("您暂时还没有下订单的权限。");
                break;
            case R.id.lyt_di_nuan:
                if((OrderDict.RoleType.TYPE_NQ.equals(role) && Constants.TYPE_DI_NUAN.equals(type)) || OrderDict.RoleType.TYPE_GRKH.equals(role)) {
                    ActivityUtils.startActivity(getActivity(), DiNuanMarkOrderActivity.class);
                }else
                    ActivityUtils.toast("您暂时还没有下订单的权限。");
                break;

        }

    }



    private void initAutoBanner() {
        RequestManager.getInstance().call(new BannerListApi(new BannerListApi.BannerListParams(Constants.BANNER_HOME),
                respBannerListener, errorListener));
    }


    Response.Listener<BannerBean[]> respBannerListener = new Response.Listener<BannerBean[]>() {

        @Override
        public void onResponse(BannerBean[] response) {

            if (HandleCode.requestSuccess() && response.length > 0) {

                List<String> imgesUrl = new ArrayList<>();
                for (int i = 0; i < response.length; i++) {
                    imgesUrl.add(ApiUrl.getDefaultHost() + response[i].getPath());
                }
                autoBanner.setImagesUrl(imgesUrl);
                autoBanner.setOnItemClickListener(new AutoBanner.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {

                        ActivityUtils.toast("" + position);

                    }
                });
                Log.i("juner", "juner respBannerListener response : " + GsonHelper.writeValue(imgesUrl));
            } else {
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };

    @Override
    public void initView() {

        if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
            // 用户
        } else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGGR)){
            // 施工工人
        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_JCY)){
            // 监察员
        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_NQ)){
            // 内勤
        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGDZ)){
            // 施工队长
        }
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

        initAutoBanner();

        if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
            // 用户

        } else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGGR)){
            // 施工工人

        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_JCY)){
            // 监察员

        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_NQ)){
            // 内勤


        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGDZ)){
            // 施工队长

        }


    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }
}
